package com.ist412.pocke.SQL;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.ist412.pocke.DTO.WorkoutDTO;

public class Workout {
	
	//TableName
	private static final String TABLE_NAME= "Workout";
	
	//Workout Indices
	private static final int ID_INDEX = 0;
	private static final int WORKOUT_NAME_INDEX = 1;
	private static final int SCHEDULED_INDEX = 2;
	private static final int WORKOUT_TYPE_ID_INDEX = 3;
	private static final int SCHEDULE_INDEX = 4;
	
	//Workout Keys
	private static final String WORKOUT_NAME_KEY = "workoutName";
	private static final String SCHEDULED_KEY = "scheduled";
	private static final String WORKOUT_TYPE_KEY = "WORKOUTTYPE_ID";
	private static final String SCHEDULE_KEY = "schedule";
	
	
	public static ArrayList<WorkoutDTO> getWorkouts(Context context){
		SQLMachine sql = new SQLMachine(context);
		SQLiteDatabase db = sql.getReadableDatabase();
		ArrayList<WorkoutDTO> workouts = new ArrayList<WorkoutDTO>();
		String query = "SELECT * FROM Workout";
		Cursor cursor = db.rawQuery(query, new String[] {});
		while(cursor.moveToNext()){
			WorkoutDTO workout = new WorkoutDTO();
			workout.setId(cursor.getInt(ID_INDEX));
			workout.setWorkoutName(cursor.getString(WORKOUT_NAME_INDEX));
			workout.setScheduled(cursor.getInt(SCHEDULED_INDEX));
			workout.setWorkoutTypeID(cursor.getInt(WORKOUT_TYPE_ID_INDEX));
			workout.setSchedule(cursor.getString(SCHEDULE_INDEX));
			workouts.add(workout);
		}
		return workouts;
	}
	
	public static WorkoutDTO getWorkout(Context context, int workoutId){
		SQLMachine sql = new SQLMachine(context);
		SQLiteDatabase db = sql.getReadableDatabase();
		String query = "SELECT * FROM Workout WHERE id=?";
		Cursor cursor = db.rawQuery(query, new String[] {String.valueOf(workoutId)});
		WorkoutDTO workout = new WorkoutDTO();
		if(cursor.moveToFirst()){
			workout.setId(cursor.getInt(ID_INDEX));
			workout.setWorkoutName(cursor.getString(WORKOUT_NAME_INDEX));
			workout.setScheduled(cursor.getInt(SCHEDULED_INDEX));
			workout.setWorkoutTypeID(cursor.getInt(WORKOUT_TYPE_ID_INDEX));
			workout.setSchedule(cursor.getString(SCHEDULE_INDEX));
		}
		return workout;
	}
	
	public static void createWorkout(Context context, WorkoutDTO workout){
		SQLMachine sql = new SQLMachine(context);
		SQLiteDatabase db = sql.getReadableDatabase();
		ContentValues values = new ContentValues();
		values.put(WORKOUT_NAME_KEY, workout.getWorkoutName());
		values.put(SCHEDULED_KEY, workout.getScheduled());
		values.put(WORKOUT_TYPE_KEY, workout.getWorkoutTypeID());
		values.put(SCHEDULE_KEY, workout.getSchedule());
		db.insert(TABLE_NAME, null, values);
	}
	
	public static void updateWorkout(Context context, WorkoutDTO workout){
		SQLMachine sql = new SQLMachine(context);
		SQLiteDatabase db = sql.getReadableDatabase();
		ContentValues values = new ContentValues();
		values.put(WORKOUT_NAME_KEY, workout.getWorkoutName());
		values.put(SCHEDULED_KEY, workout.getScheduled());
		values.put(WORKOUT_TYPE_KEY, workout.getWorkoutTypeID());
		values.put(SCHEDULE_KEY, workout.getSchedule());
		String filter = "id=" + workout.getId();
		db.update(TABLE_NAME, values, filter, null);
	}
	
	public static void deleteWorkout(Context context, int id){
		SQLMachine sql = new SQLMachine(context);
		SQLiteDatabase db = sql.getReadableDatabase();
		String filter = "id=" + id;
		db.delete(TABLE_NAME, filter, null);
	}

}
